package thread_sum;

import java.util.Random;

public class Test {
    private static long sum1 = 0;
    private static long sum2 = 0;
    private static int pos = 0;
    public static void main(String[] args) throws InterruptedException {
        int[] arr = new int[10_000_000];
        long starttime = System.currentTimeMillis();
        for (int i = 0; i < arr.length ; i++) {
            Random random = new Random();
            arr[i] = random.nextInt(100) + 1;
        }
        Thread t1 = new Thread(()->{
            while (pos < arr.length){
                if(arr[pos] % 2 == 0){
                    sum1 += arr[pos];
                }
                pos++;
            }
        });
        Thread t2 = new Thread(()->{
            while (pos < arr.length){
                if (arr[pos] % 2 != 0){
                    sum2 += arr[pos];
                }
                pos++;
            }
        });
        t1.start();
        t1.join();
        pos = 0;
        t2.start();
        t2.join();
        long sum = sum1 + sum2;
        long endtime = System.currentTimeMillis();
        System.out.println("数组中元素和为:->" + sum);
        System.out.println("程序执行时间为:->" + (endtime - starttime));
    }
}
